#include <iostream>
using namespace std;
typedef long long LL;

const int N = 100010;
int a[N];
int tem[N];
int n;

LL merge_sort(int a[], int l, int r)
{
    if (l >= r)
        return 0;
    int mid = l + r >> 1;
    LL ans = merge_sort(a, l, mid) + merge_sort(a, mid + 1, r);
    int i = l, j = mid + 1, k = 0;
    while (i <= mid && j <= r)
        if (a[i] <= a[j])
            tem[k++] = a[i++];
        else
            tem[k++] = a[j++], ans += mid - i + 1;
    while (i <= mid)
        tem[k++] = a[i++];
    while (j <= r)
        tem[k++] = a[j++];
    for (i = l, j = 0; i <= r; ++i, ++j)
        a[i] = tem[j];
    return ans;
}

int main()
{
    scanf("%d", &n);
    for (int i = 0; i < n; ++i)
        scanf("%d", a + i);
    printf("%lld\n", merge_sort(a, 0, n - 1));
    for (int i = 0; i < n; ++i)
        printf("%d ", a[i]);
    return 0;
}